{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-07T16:18:30.587801Z",
     "start_time": "2025-08-07T16:18:30.574619Z"
    }
   },
   "source": [
    "#读取csv\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "#数据导入\n",
    "df = pd.read_csv(\"../课件/data/employees.csv\")\n",
    "# print(df)\n",
    "print(type(df)) #\n",
    "print(df.head())\n",
    "#数据导出\n",
    "df = df.tail(5)\n",
    "df.to_csv(\"../课件/data/employees_out.csv\")\n",
    "# df.to_csv(\"./employees_out.csv\")\n",
    "print(\"ok\")\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "   employee_id first_name last_name     email  phone_number   job_id   salary  \\\n",
      "0          100     Steven      King     SKING  515.123.4567  AD_PRES  24000.0   \n",
      "1          101      N_ann   Kochhar  NKOCHHAR  515.123.4568    AD_VP  17000.0   \n",
      "2          102        Lex   De Haan   LDEHAAN  515.123.4569    AD_VP  17000.0   \n",
      "3          103  Alexander    Hunold   AHUNOLD  590.423.4567  IT_PROG   9000.0   \n",
      "4          104      Bruce     Ernst    BERNST  590.423.4568  IT_PROG   6000.0   \n",
      "\n",
      "   commission_pct  manager_id  department_id  \n",
      "0             NaN         NaN           90.0  \n",
      "1             NaN       100.0           90.0  \n",
      "2             NaN       100.0           90.0  \n",
      "3             NaN       102.0           60.0  \n",
      "4             NaN       103.0           60.0  \n",
      "ok\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-17T12:11:42.102626Z",
     "start_time": "2025-08-17T12:11:39.510566Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#读取json\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "df = pd.read_json(\"../课件/data/data1.json\")\n",
    "print(df)\n",
    "\n",
    "import json\n",
    "with open('../课件/data/test.json',encoding='utf-8') as f :\n",
    "    data = json.load(f)\n",
    "    print(data)\n",
    "    print(type(data))\n",
    "df = pd.DataFrame(data['users'])\n",
    "print(df)"
   ],
   "id": "2b02e8c0bd678219",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id name  age\n",
      "0   1   张三   25\n",
      "1   2   李四   30\n",
      "2   3   王五   28\n",
      "{'users': [{'id': 1, 'name': '张三', 'age': 28, 'email': 'zhangsan@example.com', 'is_active': True, 'join_date': '2022-03-15'}, {'id': 2, 'name': '李四', 'age': 35, 'email': 'lisi@example.com', 'is_active': False, 'join_date': '2021-11-02'}, {'id': 3, 'name': '王五', 'age': 24, 'email': 'wangwu@example.com', 'is_active': True, 'join_date': '2023-01-20'}]}\n",
      "<class 'dict'>\n",
      "   id name  age                 email  is_active   join_date\n",
      "0   1   张三   28  zhangsan@example.com       True  2022-03-15\n",
      "1   2   李四   35      lisi@example.com      False  2021-11-02\n",
      "2   3   王五   24    wangwu@example.com       True  2023-01-20\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-10T13:27:52.677939Z",
     "start_time": "2025-08-10T13:27:52.640747Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 缺失值的处理\n",
    "# nan:not a number\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series([12,25,np.nan, None, pd.NA])\n",
    "df = pd.DataFrame([[1,pd.NA,2],[2,3,5],[None,4,6]],columns=['第1列','第2列','第3列'])\n",
    "print(s)\n",
    "# 查看是否是缺失值\n",
    "print(s.isna())\n",
    "print(s.isnull())\n",
    "print(df.isna())\n",
    "print(df.isnull())\n",
    "print(df.isna().sum(axis=1))\n",
    "print(s.isna().sum()) #查看缺失值的个数\n",
    "\n",
    "# 剔除缺失值\n",
    "print(s.dropna())\n",
    "print('-'*30)\n",
    "print(df)\n",
    "print(df.dropna()) #剔除一整条的记录\n",
    "print(df.dropna(how='all')) #如果所有的值都是缺失值，删除这一行\n",
    "print(df.dropna(thresh=1)) #如果至少有n个值不是缺失值，就保留\n",
    "print(df.dropna(axis=1)) #剔除一整列的记录\n",
    "print(df.dropna(subset=['第1列'])) #如果某列有缺失值，则删除这一行\n",
    "\n",
    "# 填充缺失值\n",
    "print(\"----------填充缺失值------------------\")\n",
    "df = pd.read_csv('../课件/data/weather_withna.csv')\n",
    "df.tail()\n",
    "df.isna().sum(axis=0)\n",
    "df.head()\n",
    "print(df.tail())\n",
    "# print(df.fillna({'temp_max':20,'wind':2.5}).tail()) #使用字典来填充\n",
    "# print(df.fillna(df[['temp_max','wind']].mean()).tail()) #使用统计值来填充\n",
    "print(df.ffill().tail())#用前面的第一个非空相邻值填充\n",
    "print(df.bfill().tail())#用后面的第一个非空相邻值填充"
   ],
   "id": "eda267c4be9e018e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      12\n",
      "1      25\n",
      "2     NaN\n",
      "3    None\n",
      "4    <NA>\n",
      "dtype: object\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "dtype: bool\n",
      "     第1列    第2列    第3列\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "     第1列    第2列    第3列\n",
      "0  False   True  False\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "0    1\n",
      "1    0\n",
      "2    1\n",
      "dtype: int64\n",
      "3\n",
      "0    12\n",
      "1    25\n",
      "dtype: object\n",
      "------------------------------\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第1列 第2列  第3列\n",
      "1  2.0   3    5\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "2  NaN     4    6\n",
      "   第3列\n",
      "0    2\n",
      "1    5\n",
      "2    6\n",
      "   第1列   第2列  第3列\n",
      "0  1.0  <NA>    2\n",
      "1  2.0     3    5\n",
      "----------填充缺失值------------------\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            NaN       NaN       NaN   NaN     NaN\n",
      "1457  2015-12-28            NaN       NaN       NaN   NaN     NaN\n",
      "1458  2015-12-29            NaN       NaN       NaN   NaN     NaN\n",
      "1459  2015-12-30            NaN       NaN       NaN   NaN     NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27            0.0      11.1       4.4   4.8     sun\n",
      "1457  2015-12-28            0.0      11.1       4.4   4.8     sun\n",
      "1458  2015-12-29            0.0      11.1       4.4   4.8     sun\n",
      "1459  2015-12-30            0.0      11.1       4.4   4.8     sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n",
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1456  2015-12-27           20.6      12.2       5.0   3.8    rain\n",
      "1457  2015-12-28           20.6      12.2       5.0   3.8    rain\n",
      "1458  2015-12-29           20.6      12.2       5.0   3.8    rain\n",
      "1459  2015-12-30           20.6      12.2       5.0   3.8    rain\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8    rain\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df.duplicated()  #一整条记录都是一样的，标记为重复，返回True\n",
    "df.drop_duplicates(subset=['name'])  #根据指定列去重\n",
    "df.drop_duplicates(subset=['name'], keep='last')  #保留最后一次出现的行\n",
    "# 数据类型的转换\n",
    "df = pd.read_csv('../课件/data/sleep.csv')\n",
    "df.dtypes\n",
    "df['age'] = df['age'].astype('int16')\n",
    "df['gender'] = df['gender'].astype('category')\n",
    "df.gender\n",
    "df['is_male'] = df['gender'].map({'Female': True, 'Male': False})\n",
    "df.is_male"
   ],
   "id": "4a2f9ed474c05f7d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "data = {\n",
    "    'ID': [1, 2],\n",
    "    'name':['alice smith','bob smith'],\n",
    "    'Math': [90, 85],\n",
    "    'English': [88, 92],\n",
    "    'Science': [95, 89]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "# 分列\n",
    "df[['first','last']]  = df['name'].str.split(\" \",expand=True)\n",
    "df = pd.read_csv('../课件/data/sleep.csv')\n",
    "df = df[['person_id','blood_pressure']]\n",
    "df[['high','low']] = df['blood_pressure'].str.split('/',expand=True)\n",
    "df['high']=df['high'].astype('int64')\n",
    "df['low']=df['low'].astype('int64')\n",
    "df.info()\n",
    "df.high.mean()\n",
    "df.low.mean()"
   ],
   "id": "2eedd1491073e1b"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 数据分箱 pd.cut(x,bins,labels)\n",
    "import pandas as pd\n",
    "df = pd.read_csv('../课件/data/employees.csv')\n",
    "df.head(10)\n",
    "\n",
    "df1 = df.head(10)[['employee_id','salary']]\n",
    "\n",
    "pd.cut(df1['salary'],bins=3) #bins=n，分成n段区间，起始值、结束值是所有数据的最小值、最大值\n",
    "#4180~14100~24000\n",
    "pd.cut(df1['salary'],bins=3).value_counts()\n",
    "pd.cut(df1['salary'],bins=[0,10000,20000,30000])#bins=list，分成n段区间\n",
    "pd.cut(df1['salary'],bins=[0,10000,20000,30000]).value_counts()\n",
    "df1['收入范围'] =pd.cut(df1['salary'],bins=[0,10000,20000,30000],labels=['低','中','高'])#bins=list，分成n段区间\n",
    "pd.qcut(df1['salary'],3).value_counts()\n"
   ],
   "id": "9718c04def748025"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# df.rename()   df.set_index()  df.reset_index()\n",
    "df = pd.DataFrame({\n",
    "    'name':['jack','alice','tom','bob'],\n",
    "    'age':[20,30,40,50],\n",
    "    'gender':['female','male','female','male']\n",
    "})\n",
    "df.set_index(\"name\",inplace=True) #设置索引\n",
    "df.reset_index(inplace=True) #重置索引\n",
    "df.rename(columns={\"age\":\"年龄\"},index={0:4}) #重命名列和索引\n"
   ],
   "id": "8c2d6a7995adc307"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 时间数据的处理\n",
    "import pandas as pd\n",
    "d = pd.Timestamp('2015-02-28 10:22')\n",
    "d1 = pd.Timestamp('2015-02-28 13:22')\n",
    "print(d)\n",
    "print(type(d))\n",
    "print(\"年：\",d.year)\n",
    "print(\"月：\",d.month)\n",
    "print(\"日：\",d.day)\n",
    "print(d.hour, d.minute, d.second)\n",
    "print(\"季度：\",d.quarter)\n",
    "print(\"是否是月底：\",d.is_month_end)\n",
    "# 方法\n",
    "print(\"星期几：\",d.day_name())\n",
    "print(\"转换为天：\",d.to_period(\"D\"))\n",
    "print(\"转换为季度：\",d1.to_period(\"Q\"))\n",
    "print(\"转换为年度：\",d1.to_period(\"Y\"))\n",
    "print(\"转换为月度：\",d1.to_period(\"M\"))\n",
    "print(\"转换为周维度：\",d1.to_period(\"W\"))"
   ],
   "id": "20073bfa2db67e4d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 字符串转换为日期类型\n",
    "a = pd.to_datetime('20150228')\n",
    "print(a)\n",
    "print(type(a))\n",
    "print(a.day_name())\n",
    "\n",
    "# dataFrame 日期转换\n",
    "df = pd.DataFrame({\n",
    "    'sales':[100,200,300],\n",
    "    'date':['20250601','20250602','20250603']\n",
    "})\n",
    "df['datetime'] = pd.to_datetime(df['date'])\n",
    "df\n",
    "print(df.info())\n",
    "print(type(df['datetime']))\n",
    "df['week']=df['datetime'].dt.day_name()\n",
    "df['datetime'].dt.year\n"
   ],
   "id": "843613eb7a1c6697"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# csv 日期转换 parse_dates\n",
    "df = pd.read_csv('../课件/data/weather.csv',parse_dates=['date'])\n",
    "df.info()\n",
    "df['date'].dt.day_name()"
   ],
   "id": "1cda125e0ebe33c2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 日期数据作为索引\n",
    "# df.set_index('date' , inplace=True)#设置原来的df的索引\n",
    "print(df.loc[\"2013-01\":\"2013-02\"])"
   ],
   "id": "f6941eee7cf59239"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 时间间隔\n",
    "d1 = pd.Timestamp('2013-01-15')\n",
    "d2 = pd.Timestamp('2023-02-23')\n",
    "d3 = d2-d1\n",
    "print(type(d3))\n",
    "print(d3)"
   ],
   "id": "c454e84fe5b3e74d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df = pd.read_csv('../课件/data/weather.csv',parse_dates=['date'])\n",
    "df.info()\n",
    "df['delta'] = df['date'] - df['date'][0]\n",
    "df.set_index('delta',inplace=True)\n",
    "df\n",
    "print(df.loc['10 days':'20 days'])"
   ],
   "id": "5638301e38ef1a47"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df = pd.read_csv('../课件/data/weather.csv',parse_dates=['date'])\n",
    "# 重新采样\n",
    "df.set_index('date',inplace=True)"
   ],
   "id": "b91226f9406edb8f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "days = pd.date_range(\"2025-07-03\",\"2026-02-09\",freq=\"W\")\n",
    "days = pd.date_range(\"2025-07-03\",periods=10,freq=\"QE\")\n",
    "print(days)\n",
    "df[ [\"temp_max\",\"temp_min\"]].resample# 分组聚合\n",
    "# df.groupby('分组的字段')['聚合的字段'].聚合函数()\n",
    "import pandas as pd\n",
    "df = pd.read_csv('../课件/data/employees.csv')\n",
    "df = df.dropna(subset=['department_id'])\n",
    "df['department_id'] = df['department_id'].astype('int64')\n",
    "# 计算不同部门的平均薪资\n",
    "df.groupby('department_id').groups #查看分组\n",
    "df.groupby('department_id').get_group(20) #查看具体的某个分组数据\n",
    "df2 = df.groupby('department_id')[['salary']].mean()\n",
    "df2['salary'] = df2['salary'].round(2)\n",
    "df2=df2.reset_index()\n",
    "df2.sort_values('salary',ascending=False)(\"MS\").mean()\n",
    "df[ [\"temp_max\",\"temp_min\"]].resample(\"YE\").mean()\n"
   ],
   "id": "857a8122d666cc95"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 计算不同部门不同岗位的人的平均薪资\n",
    "df2=df.groupby(['department_id','job_id'])[['salary']].mean()\n",
    "df2=df2.reset_index()\n",
    "df2['salary'] = df2['salary'].round(1)\n",
    "df2.sort_values('salary',ascending=False)"
   ],
   "id": "df2a912444e54025"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-11T18:32:03.087808Z",
     "start_time": "2025-08-11T18:32:03.060090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 企鹅数据分析\n",
    "# 1. 导入必要的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 2. 导入数据 喙\n",
    "df = pd.read_csv('../课件/data/penguins.csv')\n",
    "df.head(5)\n",
    "df.info()\n"
   ],
   "id": "eef4d08cc6838b0e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 344 entries, 0 to 343\n",
      "Data columns (total 7 columns):\n",
      " #   Column             Non-Null Count  Dtype  \n",
      "---  ------             --------------  -----  \n",
      " 0   species            344 non-null    object \n",
      " 1   island             344 non-null    object \n",
      " 2   bill_length_mm     342 non-null    float64\n",
      " 3   bill_depth_mm      342 non-null    float64\n",
      " 4   flipper_length_mm  342 non-null    float64\n",
      " 5   body_mass_g        342 non-null    float64\n",
      " 6   sex                333 non-null    object \n",
      "dtypes: float64(4), object(3)\n",
      "memory usage: 18.9+ KB\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 3. 数据清洗\n",
    "# 缺失值的检查\n",
    "print(df.isna().sum())\n",
    "df.dropna(inplace=True)"
   ],
   "id": "2de6f0377a50b3cc"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 4. 数据特征的构造\n",
    "df['sex'] = df['sex'].astype('category')\n",
    "df['bill_ratio'] = df['bill_length_mm']/df['bill_depth_mm']\n",
    "df.head()"
   ],
   "id": "9d63e745cf5428d8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 5. 数据分析\n",
    "# 数据分箱-把体重分为三个等级\n",
    "labels = ['低','中','高']\n",
    "df['mass_level'] = pd.cut(df['body_mass_g'],bins=3,labels=labels)\n",
    "print(df['mass_level'].value_counts())\n",
    "# 按岛屿、性别分组分析，ag统计多种属性\n",
    "df.groupby(['sex','island']).agg({\n",
    "    'body_mass_g':['mean','count'],\n",
    "})"
   ],
   "id": "9780d170b972bc0b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-11T18:32:06.910460Z",
     "start_time": "2025-08-11T18:32:06.879635Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 睡眠质量分析\n",
    "# 1.导入库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 2.导入数据\n",
    "df = pd.read_csv('../课件/data/sleep.csv')\n",
    "df.head()\n",
    "df.info()\n",
    "df.describe()"
   ],
   "id": "fdc7cb8f7549e6b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 400 entries, 0 to 399\n",
      "Data columns (total 13 columns):\n",
      " #   Column                   Non-Null Count  Dtype  \n",
      "---  ------                   --------------  -----  \n",
      " 0   person_id                400 non-null    int64  \n",
      " 1   gender                   400 non-null    object \n",
      " 2   age                      400 non-null    int64  \n",
      " 3   occupation               400 non-null    object \n",
      " 4   sleep_duration           400 non-null    float64\n",
      " 5   sleep_quality            400 non-null    float64\n",
      " 6   physical_activity_level  400 non-null    int64  \n",
      " 7   stress_level             400 non-null    int64  \n",
      " 8   bmi_category             400 non-null    object \n",
      " 9   blood_pressure           400 non-null    object \n",
      " 10  heart_rate               400 non-null    int64  \n",
      " 11  daily_steps              400 non-null    int64  \n",
      " 12  sleep_disorder           110 non-null    object \n",
      "dtypes: float64(2), int64(6), object(5)\n",
      "memory usage: 40.8+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "        person_id         age  sleep_duration  sleep_quality  \\\n",
       "count  400.000000  400.000000      400.000000     400.000000   \n",
       "mean   200.500000   39.950000        8.041250       6.125750   \n",
       "std    115.614301   14.038883        2.390787       1.975733   \n",
       "min      1.000000   18.000000        4.100000       1.000000   \n",
       "25%    100.750000   29.000000        5.900000       4.700000   \n",
       "50%    200.500000   40.000000        8.200000       6.100000   \n",
       "75%    300.250000   49.000000       10.125000       7.425000   \n",
       "max    400.000000   90.000000       12.000000      10.000000   \n",
       "\n",
       "       physical_activity_level  stress_level  heart_rate   daily_steps  \n",
       "count               400.000000     400.00000  400.000000    400.000000  \n",
       "mean                 64.985000       5.47250   75.990000  11076.510000  \n",
       "std                  32.297874       2.80873   15.099334   5364.789364  \n",
       "min                  10.000000       1.00000   50.000000   2067.000000  \n",
       "25%                  35.000000       3.00000   63.000000   6165.250000  \n",
       "50%                  65.500000       5.00000   77.000000  11785.500000  \n",
       "75%                  94.000000       8.00000   90.000000  15878.000000  \n",
       "max                 120.000000      10.00000  100.000000  19958.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>person_id</th>\n",
       "      <th>age</th>\n",
       "      <th>sleep_duration</th>\n",
       "      <th>sleep_quality</th>\n",
       "      <th>physical_activity_level</th>\n",
       "      <th>stress_level</th>\n",
       "      <th>heart_rate</th>\n",
       "      <th>daily_steps</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.00000</td>\n",
       "      <td>400.000000</td>\n",
       "      <td>400.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>200.500000</td>\n",
       "      <td>39.950000</td>\n",
       "      <td>8.041250</td>\n",
       "      <td>6.125750</td>\n",
       "      <td>64.985000</td>\n",
       "      <td>5.47250</td>\n",
       "      <td>75.990000</td>\n",
       "      <td>11076.510000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>115.614301</td>\n",
       "      <td>14.038883</td>\n",
       "      <td>2.390787</td>\n",
       "      <td>1.975733</td>\n",
       "      <td>32.297874</td>\n",
       "      <td>2.80873</td>\n",
       "      <td>15.099334</td>\n",
       "      <td>5364.789364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>4.100000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>2067.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>100.750000</td>\n",
       "      <td>29.000000</td>\n",
       "      <td>5.900000</td>\n",
       "      <td>4.700000</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>3.00000</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>6165.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>200.500000</td>\n",
       "      <td>40.000000</td>\n",
       "      <td>8.200000</td>\n",
       "      <td>6.100000</td>\n",
       "      <td>65.500000</td>\n",
       "      <td>5.00000</td>\n",
       "      <td>77.000000</td>\n",
       "      <td>11785.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>300.250000</td>\n",
       "      <td>49.000000</td>\n",
       "      <td>10.125000</td>\n",
       "      <td>7.425000</td>\n",
       "      <td>94.000000</td>\n",
       "      <td>8.00000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>15878.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>400.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>120.000000</td>\n",
       "      <td>10.00000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>19958.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 3.数据清洗\n",
    "df.isna().sum()\n",
    "df.drop(columns='sleep_disorder',inplace=True)"
   ],
   "id": "b21c2beaab50af6a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 4. 数据特征的构造\n",
    "df['gender'] = df['gender'].astype('category')\n",
    "df['occupation'] = df['occupation'].astype('category')\n",
    "df['bmi_category'] = df['bmi_category'].astype('category')\n",
    "df[['high','low']]=df['blood_pressure'].str.split('/',expand=True)\n",
    "\n",
    "# 睡眠质量的分箱\n",
    "labels = ['差','中','优']\n",
    "df['quality_level'] = pd.cut(df['sleep_quality'],bins=3,labels=labels)\n",
    "age_labels=['青少年','中年','老年']\n",
    "df['age_level'] = pd.cut(df['age'],bins=3,labels=age_labels)\n",
    "df.head()"
   ],
   "id": "cae73882d9b61108"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-11T18:32:10.529034Z",
     "start_time": "2025-08-11T18:32:10.516928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5.数据的统计、分析\n",
    "print(df['bmi_category'].value_counts())"
   ],
   "id": "9edd243dedef5093",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bmi_category\n",
      "Overweight     109\n",
      "Underweight    102\n",
      "Obese           98\n",
      "Normal          91\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 根据不同的bmi分组，睡眠质量\n",
    "df.groupby(['age_level','bmi_category']).agg({\n",
    "    'sleep_duration':'mean',\n",
    "    'sleep_quality':'mean',\n",
    "    'stress_level':'mean'\n",
    "})"
   ],
   "id": "6852d5e6dc01d2bd"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "todo v53",
   "id": "102418da7b12a7cf"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
